package jp.sourceforge.akjrcp.ffmpeg;

import java.io.File;

import jp.sourceforge.akjrcp.audio.WavAudioSettingComposite;

public class WavFFMpegPreference extends AdvancedAudioFFMpegPreference {

	
	public String[] toCommand(FFMpegInput[] inputs,String output){
		FFMpegFLVParser parser=new FFMpegFLVParser(this.getFfmpegPath());
		String path=inputs[inputs.length-1].getPath();//last one
		for (int i = inputs.length-1; i >=0;i--) {
			FLVInfomation info=parser.parseFile(new File(inputs[i].getPath()));
			if(info.isHaveAudio() ){
				path=inputs[i].getPath();//this is audio
				break;
			}
		}
			return customToCommand(path, null,output);
		}

	
	public String[] customToCommand(String input,String avsInput,String output){
		FFMpegFLVParser parser=new FFMpegFLVParser(this.getFfmpegPath());
		FLVInfomation info=parser.parseFile(new File(input));
		
		//audio error check
		if(!info.isHaveAudio() ){
			if(avsInput==null){
			log.warn("no aduio stream");
			return null;
			}else{
				info=parser.parseFile(new File(avsInput));
				if(!info.isHaveAudio() ){
					log.warn("no aduio stream even in avs");
					return null;
					}
			}
		}
		
		log.info("info:"+info.getAudioRate()+","+info.getAudioChannel());
		if(!isForceConvert()){
			//same as source
			this.setAudioRate(info.getAudioRate());
			this.setAudioChannel(info.getAudioChannel());
		}else{
			if(getConvertChannel()==WavAudioSettingComposite.SAME_SOURCE){
				this.setAudioChannel(info.getAudioChannel());
			}else{
				this.setAudioChannel(getConvertChannel());
			}
			
			if(getConvertFramerate()==WavAudioSettingComposite.SAME_SOURCE){
				this.setAudioRate(info.getAudioRate());
			}else{
				this.setAudioRate(getConvertFramerate());
			}
		}
		int bps=this.getAudioRate()*this.getAudioChannel()*16/1000;
		
		this.setGuessSize(FFMpegPreference.toKBytes(info.getDuration(), bps));
		

		if(input==null || output==null){
			//should exception
			return null;
		}
	FFMpegInput inputs[]=new FFMpegInput[1];
		if(avsInput==null){
			
				inputs[0]=new FFMpegInput(input);
			}else{
				inputs[0]=new FFMpegInput(avsInput);
			}
		//call super input
		return super.toCommand(inputs,output);
	}
}
